package com.service.impl;

import com.dao.CommentDao;
import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;
import com.entity.Comment;
import com.page.PageInfo;
import com.service.BaseService;
import com.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Service
public class CommentServiceImpl extends BaseService implements CommentService {
    @Autowired
    CommentDao commentDao;
    /**
     *
     * @param pid 父id 常量的类别id
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public BaseDto getCommentByPid(int pid,String cacheId) throws SQLException, ClassNotFoundException{
        BaseDto dto  = new BaseDto();
        List<Comment> comments = commentDao.getCommentByPid(pid,cacheId);
        msg = Optional.ofNullable(comments).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
        code = Optional.ofNullable(comments).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
        dto.setCode(code);
        dto.setMsg(msg);
        dto.settList(comments);
      return dto;
    };


    /**
     * 增加一个常量
     *
     * @param comment
     *            常量对象
     * @return 返回插入的状态值 数字 为1成功 0失败 -值异常
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws SQLException
     *             抛出sql异常
     */
    public BaseDto addComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException{
        BaseDto dto  = new BaseDto();
        int result  =commentDao.addComment(comment);
        msg = result>0?MessageEnum.ADD_COMMENT_SUCCESS.getMsg():MessageEnum.ADD_COMMENT_FAIL.getMsg();
        code = result>0?MessageEnum.ADD_COMMENT_SUCCESS.getCode():MessageEnum.ADD_COMMENT_SUCCESS.getCode();
        dto.setCode(code);
        dto.setMsg(msg);
        return dto;
    }

    /**
     * 修改常量信息
     * @param comment
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    public BaseDto updateComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException{
        BaseDto dto  = new BaseDto();
        int result  =commentDao.updateComment(comment);
        msg = result>0?MessageEnum.UPDATE_COMMENT_SUCCESS.getMsg():MessageEnum.UPDATE_COMMENT_FAIL.getMsg();
        code = result>0?MessageEnum.UPDATE_COMMENT_SUCCESS.getCode():MessageEnum.UPDATE_COMMENT_FAIL.getCode();
        dto.setCode(code);
        dto.setMsg(msg);
        return dto;
    };

    /**
     * 根据主键删除
     * @param coid
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    public BaseDto deleteComment(int coid) throws IllegalArgumentException, IllegalAccessException, SQLException{
        BaseDto dto  = new BaseDto();
        int result  =commentDao.deleteComment(coid);
        msg = result>0?MessageEnum.DELETE_COMMENT_SUCCESS.getMsg():MessageEnum.DELETE_COMMENT_FAIL.getMsg();
        code = result>0?MessageEnum.DELETE_COMMENT_SUCCESS.getCode():MessageEnum.DELETE_COMMENT_FAIL.getCode();
        dto.setCode(code);
        dto.setMsg(msg);
        return dto;
    };

    /**
     * 根据常量分类 分页查询，分页查询 就是 mysql的limit 语句需要2个参数 参数1从几开始取 参数2 取多长
     * @param pageInfo
     * @return
     * @throws Exception
     */
    public BaseDto queryCommentsByPage(PageInfo pageInfo, int pid) throws Exception {
        BaseDto dto  = new BaseDto();
        List<Comment> comments = commentDao.queryCommentsByPage(pageInfo,pid);
        msg = Optional.ofNullable(comments).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getMsg():MessageEnum.SELECT_COMMENT_FAIL.getMsg();
        code = Optional.ofNullable(comments).isPresent()?MessageEnum.SELECT_COMMENT_SUCCESS.getCode():MessageEnum.SELECT_COMMENT_FAIL.getCode();
        dto.setCode(code);
        dto.setMsg(msg);
        dto.settList(comments);
        return dto;
    }

}
